package com.simba.server.commands.impl.fetch;

import java.sql.ResultSet;
import com.simba.common.command.ICommand;
import com.simba.common.database.DbOperator;
import com.simba.server.components.SorterParams;
import com.simba.server.components.data.BoxInfo;

public class FetchBoxPackCmd implements ICommand
{
	public static final String NAME = "FetchBoxPackCmd";
	private final BoxInfo boxInfo;
	private final String sql = "select id,boxCode,uploadNum,slotId,boxType,createTime,roadSlot,planId  "
			+ "from tt_slot_pack_info where uploadFlag=0 and uploadNum<? order by id asc limit 1;";

	public FetchBoxPackCmd(BoxInfo boxInfo)
	{
		this.boxInfo = boxInfo;
	}

	@Override
	public String getName()
	{
		return FetchBoxPackCmd.NAME;
	}

	@Override
	public Boolean run(Object params)
	{
		Boolean ret = false;

		if (!(params instanceof DbOperator))
		{
			return ret;
		}

		try
		{
			DbOperator dbOperator = (DbOperator) params;

			dbOperator.prepareStatement(this.sql);
			dbOperator.setInt(1, SorterParams.getInstance().getUploadNum());

			ResultSet rs = dbOperator.executeQuery();

			if (rs.next())
			{
				int id = rs.getInt("id");
				this.boxInfo.setId(id);

				String boxCode = rs.getString("boxCode");
				this.boxInfo.setBoxCode(boxCode);

				String planId = rs.getString("planId");
				this.boxInfo.setPlanId(planId);

				int uploadNum = rs.getInt("uploadNum");
				this.boxInfo.setUploadNum(uploadNum);

				String slotId = rs.getString("slotId");
				this.boxInfo.setSlotId(slotId);

				String boxType = rs.getString("boxType");
				this.boxInfo.setBoxType(boxType);

				String createTime = rs.getString("createTime");
				this.boxInfo.setCreateTime(createTime);

				String roadSlot = rs.getString("roadSlot");
				this.boxInfo.setRoadSlot(roadSlot);
			}

			ret = true;
		}
		catch (Exception ex)
		{
			ret = false;
		}

		return ret;
	}

}
